package test.com.qu.myapplication;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.Header;
import test.com.qu.myapplication.service.Api;
import test.com.qu.myapplication.service.NoticeUtils;


public class MainActivity extends AppCompatActivity {
    private TextView text;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        handleIntent(getIntent());
    }
    /**
     * 处理传进来的intent
     */
    private void handleIntent(Intent intent) {
        if (intent == null)
            return;
        String action = intent.getAction();
        if (intent.getBooleanExtra("NOTICE", false)) {
            notificationBarClick(intent);
        }
    }
    /**
     * 从通知栏点击的时候相应
     */
    private void notificationBarClick(Intent fromWhich) {
        if (fromWhich != null) {
            boolean fromNoticeBar = fromWhich.getBooleanExtra("NOTICE", false);
            if (fromNoticeBar) {
                Intent toMyInfor = new Intent(this, TextActivity.class);
                startActivity(toMyInfor);
            }
        }
    }
    public void initView() {
        Api.Login(new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("MainActivity","statusCode:"+statusCode);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Toast.makeText(MainActivity.this,"登录成功...",Toast.LENGTH_LONG).show();
                Api.getToken(headers);
                NoticeUtils.bindToService(MainActivity.this);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        NoticeUtils.unbindFromService(this);
        NoticeUtils.tryToShutDown(this);
    }

    public void aa(){
        NoticeUtils.unbindFromService(this);
        NoticeUtils.tryToShutDown(this);
    }

    public void bb(){
        NoticeUtils.unbindFromService(this);
        NoticeUtils.tryToShutDown(this);
    }
}
